# THIS SCRIPT COMPUTES SUMMARY STATISTICS FOR FIGURES 4 AND 8, AND
# PROVIDES THE SYNTAX USED TO GRAPH BOTH FIGURES. 
# IT MUST BE RUN IN CONJUNCTION WITH THE DATASET "ANESCumulative_cleaned.csv",
# AVAILABLE IN THE REPLICATION MATERIALS ON DATAVERSE. 
library(foreign)
library(car)
library(readstata13)
library(ggplot2)
library(stargazer)
library(grid)
library(gridExtra)
library(survey)
library(dplyr)
library(lemon)
library(ggpubr)
library(reshape2)

# READ IN THE DATA. SELECT THE FILE "ANESCumulative_cleaned.csv"
our.data = read.csv(file.choose())
####################################
####################################
####################################
####################################
####################################
####################################
####################################
####################################
# FIGURE 8: MATCHING RATES BY PARTISANSHIP, 1972-2020. 

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS, REPUBLICANS
our.props = svytable(~ year + programmatic, design = subset(our.design, republican==1))
our.props
republicans.props = prop.table(our.props, 1)
republicans.props
republicans.props = as.data.frame(republicans.props)
republicans.props
# PROPORTIONS, DEMOCRATS
our.props = svytable(~ year + programmatic, design = subset(our.design, democrat==1))
democrats.props = prop.table(our.props, 1)
democrats.props
democrats.props = as.data.frame(democrats.props)
democrats.props
# BIND THE TWO DATASETS
proportions.data = rbind(republicans.props, democrats.props)
proportions.data
proportions.data$partisanship = c(rep("Republicans", 60), 
                                  rep("Democrats", 60))
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially matched';
                                            1 = 'Matched'; else = NA")

# PLOT
our.plot = ggplot(subset(proportions.data, programmatic=="Matched"),
                  aes(year, Freq, group = partisanship, 
                      linetype =  partisanship)) +
  geom_line(aes(linetype = partisanship), linewidth = 1) +
  labs(linetype = "Partisanship") +
  theme_minimal() + 
  xlab("Year") +
  ylab("Proportion \n") + 
  # ggtitle("Matching by Partisanship, 1972-2020", 
  #        subtitle = "Data: ANES cumulative file. Lines refer to the proportion of partisan identifiers \nthat shared their party's ideological orientation and correctly placed the \nparties on the liberal-conservative spectrum.") + 
  scale_color_grey() +
  # theme(legend.title = element_text(face = "bold")) +
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure8.pdf", width = 6.375, height = 4.5)
####################################
####################################
####################################
####################################
# FIGURE 4: MATCHING, 1972-2020. 

# DESIGN
our.design = svydesign(ids=~1, weight=~our.data$VCF0009z,
                       data = our.data)

# PROPORTIONS
our.props = svytable(~ year + programmatic, design = our.design)
our.props = prop.table(our.props, 1)
our.props
proportions.data = as.data.frame(our.props)
proportions.data
# YEAR
proportions.data$year = as.numeric(as.character(proportions.data$year))
# PROGRAMMATIC
proportions.data$programmatic = car::recode(proportions.data$programmatic,
                                            "0 = 'Unmatched';
                                            0.5 = 'Partially matched';
                                            1 = 'Matched'; else = NA")

# PLOT
our.plot = ggplot(proportions.data,
                  aes(year, Freq, group = programmatic, 
                      linetype =  programmatic)) +
  geom_line(aes(linetype = programmatic), linewidth = 1) +
  labs(linetype = "Matching status") +
  xlab("Year") +
  ylab("Proportion \n") + 
  # ggtitle("Matching, 1972-2020", 
  #        subtitle = "Data: ANES cumulative file. Lines refer to the proportion of partisan identifiers composed of each group. \nRespondents are classified as matched partisans if they shared their party's ideological orientation and \ncorrectly placed the parties on the liberal-conservative spectrum.") +
  theme_minimal() +  
  scale_color_grey() +
  theme(legend.title = element_text(face = "bold")) +  
  theme(legend.position = "none") +
  # theme(legend.title = element_text(size = 12)) +
  # theme(legend.text = element_text(size = 12)) +
  theme(axis.title.x = element_text(size = 9)) + 
  theme(axis.title.y = element_text(size = 9)) +
  # theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 14)) + 
  # theme(plot.subtitle = element_text(hjust = 0.5, size = 12)) +
  theme(strip.text = element_text(face = "bold", size = 9))
our.plot

# SAVE
# ggsave(our.plot, file = "Schmidtetal-Figure4.pdf", width = 6.375, height = 4.5)
